package cn.wolfcode.cms.util;

//用户的上下文工具类
import cn.wolfcode.cms.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";


    //获取session

    public static HttpSession getSession() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }
    //将当前用户共享session中

    public static void setCurrentUser(Employee emp) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, emp);

    }

    //获取在session的当前登录用户

    public static Employee getCurrentUser() {

        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        return emp;
    }

    //将当前登录用户的权限集合共享到session中
    public static void setExpressions(List<String> list) {
        getSession().setAttribute(EXPRESSION_IN_SESSION, list);
    }

    //获取共享在session的当前登录用户的权限的集合
    public static List<String> getExpressions() {
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }


}
